<?php
class Tourism_provider_model extends CI_Model
{
    private $table = 'tourism_provider';
    
    public function findById($id)
    {
        $this->db->where('id',$id);
        return $this->db->get($this->table);
    }
    
    public function total($params = array())
    {
        if (!empty($params['username'])) {
            $this->db->where("((name LIKE '%{$params['username']}%') OR (uid = '{$params['username']}'))");
        }
        if (!empty($params['flag'])) {
            $this->db->where('flag', $params['flag']);
        }
        if (!empty($params['start_date'])) {
            $this->db->where('create_time >= ', $params['start_date'].' 00:00:00');
        }
        if (!empty($params['end_date'])) {
            $this->db->where('create_time <= ', $params['end_date'].' 23:59:59');
        }
        return $this->db->count_all_results($this->table);
    }

    public function page_list($page_num, $num, $params = array())
    {
        $this->db->select('tourism_provider.*,tourism_goods_category.name AS category_name');
        $this->db->from($this->table);
        $this->db->join('tourism_goods_category','tourism_goods_category.id = tourism_provider.category_id','left');
        if (!empty($params['username'])) {
            $this->db->where("((tourism_provider.name LIKE '%{$params['username']}%') OR (tourism_provider.uid = '{$params['username']}'))");
        }
        if (!empty($params['flag'])) {
            $this->db->where('flag', $params['flag']);
        }
        if (!empty($params['start_date'])) {
            $this->db->where('create_time >= ', $params['start_date'].' 00:00:00');
        }
        if (!empty($params['end_date'])) {
            $this->db->where('create_time <= ', $params['end_date'].' 23:59:59');
        }
        $this->db->order_by('sort', 'ASC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
     /**
     * 插入
     * @param unknown $postData
     * @param unknown $imageData
     */
    public function insertTourismProvider($postData=array(), $imageData)
    {
        $data = array(
            'uid'       => $postData['uid'],
            'name'      => $postData['name'],
            'category_id'     => $postData['category_id'],
            'company_name'    => trim($postData['company_name']),
            'main_business'   => $postData['main_business'],
            'company_address' => trim($postData['company_address']),
            'detail'    => $postData['detail'],
            'flag'      => $postData['flag'],
            'sort'      => $postData['sort'],
            'logo'      => $imageData['logoimage'],
            'wap_image' => $imageData['wapimage'],
            'site_apart'   => $postData['site_apart'],
            'service_image'      => $postData['service_image'],
            'service_mobile'      => $postData['service_mobile'],
            'service_wx' => $postData['service_wx'],
            'service_qq'   => $postData['service_qq'],
        );
        return $this->db->insert($this->table,$data);
    }
    
    /**
     * 更新
     * @param unknown $postData
     * @param unknown $id
     */
    public function updateTourismProvider($postData=array())
    {
        $data = array(
            'uid'    => $postData['uid'],
            'name'   => $postData['name'],
            'detail' => $postData['detail'],
            'flag'   => $postData['flag'],
            'sort'   => $postData['sort'],
            'category_id' => $postData['category_id'],
            'site_apart'   => $postData['site_apart'],
        );
        if (!empty($postData['logo'])) {
            $data['logo'] = $postData['logo'];
        }
        if (!empty($postData['wap_image'])) {
            $data['wap_image'] = $postData['wap_image'];
        }
        if (!empty($postData['company_name']) ){
            $data['company_name'] = $postData['company_name'];
        }
        if (!empty($postData['company_address'])){
            $data['company_address'] = $postData['company_address'];
        }
        if (!empty($postData['main_business'])){
            $data['main_business'] = $postData['main_business'];
        }
        if (!empty($postData['service_image'])) {
            $data['service_image'] = $postData['service_image'];
        }
        if (!empty($postData['service_mobile'])) {
            $data['service_mobile'] = $postData['service_mobile'];
        }
        if (!empty($postData['service_wx']) ){
            $data['service_wx'] = $postData['service_wx'];
        }
        if (!empty($postData['service_qq'])){
            $data['service_qq'] = $postData['service_qq'];
        }
        $this->db->where('id', $postData['id']);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 更新上下架
     * @param unknown $flag
     * @param unknown $id
     */
    public function updateTourismProviderFlag($flag, $id)
    {
        $data = array(
            'flag' => $flag,
        );
        if(is_array($id)) {
           $this->db->where_in('id',$id);
        } else {
           $this->db->where('id',$id);
        }
        return $this->db->update($this->table, $data);
    }
    
      /**
      * uid的获取
      * @param unknown $uid
      */
    public function getProviderByUid($uid)
    {
        $this->db->where('uid',$uid);
        return $this->db->get($this->table);
    }
    
     /**
     * 删除
     * @param unknown $id
     */
    public function deleteById($id)
    {
        $this->db->where('id',$id);
        return $this->db->delete($this->table);
    }
}